package com.sgcc.sx.pmos.e_sx_mall.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.sx.pmos.common.base.BasePresenter;
import com.sgcc.sx.pmos.common.bean.BaseResponse;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvpNetCallback;
import com.sgcc.sx.pmos.e_sx_mall.bean.InvitationListBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.InvitationListContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class InvitationListPresenter extends BasePresenter<InvitationListContract.View> implements InvitationListContract.Presenter {

    @Override
    public void queryInvitationList() {


        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_OFFER_INVITATION,
                new MvpNetCallback<InvitationListBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryInvitationListFailed();
                    }

                    @Override
                    public void onResponse(InvitationListBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }

                        if (null != response) {
                            if (response.getCode() == 200) {
                                if (null != response.getData()) {
                                    getView().queryInvitationListSuccess(response.getData());
                                }
                            } else {
                                if (!TextUtils.isEmpty(response.getMsg())) {
                                    ToastUtils.showShort(response.getMsg());
                                }
                            }
                        }

                    }
                });


    }

    @Override
    public void deleteInvitation(String guid) {
        List<String> list = new ArrayList<>();
        list.add(guid);
        Map<String, Object> params = new HashMap<>();
        params.put("guids", list);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.DELETE_INVITATION,
                new MvpNetCallback<InvitationListBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        ToastUtils.showLong("删除失败");
                    }

                    @Override
                    public void onResponse(InvitationListBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }

                        if (null != response) {
                            if (response.getCode() == 200) {
                                getView().deleteInvitationSuccess();
                            } else {
                                if (!TextUtils.isEmpty(response.getMsg())) {
                                    ToastUtils.showShort(response.getMsg());
                                }
                            }
                        }
                    }
                });


    }

    /**
     * "guid": ", //联系人ID
     * "flag": "1", //发布1，撤销0
     * "intentionStartDate": , //意向购买周期开始
     * "intentionEndDate": , //意向购买周期结束
     *
     * @param flag
     */
    @Override
    public void updateStatusInvitation(String guid, int flag, String intentionStartDate, String intentionEndDate) {


        Map<String, Object> params = new HashMap<>();
        params.put("guid", guid);
        params.put("flag", flag);
        params.put("intentionStartDate", intentionStartDate);
        params.put("intentionEndDate", intentionEndDate);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.UPDATE_STATUS_INVITATION,
                new MvpNetCallback<BaseResponse>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                    }

                    @Override
                    public void onResponse(BaseResponse response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }

                        if (null != response) {
                            if (response.isSuccess()) {
                                getView().updateStatusInvitationSuccess();
                            } else {
                                if (!TextUtils.isEmpty(response.getMsg())) {
                                    ToastUtils.showShort(response.getMsg());
                                }
                            }
                        }
                    }
                });


    }
}
